<?php


namespace App\Controller;


use SidToken\Model\InviteUserModel;
use Ucenter\Model\InviteModel;

class InviteController extends AppBaseController
{
    public function analysis()
    {
        $uid = $this->getUid();
//        $level1Amount = 0;
//        $level2Amount = 0;
//        $level3Amount = 0;


        $InviteUserModel = new InviteUserModel();
        $InviteUserModel->levelAnalysis($uid, $depth, $level1Amount, $level2Amount, $level3Amount, $level1Uids, $level2Uids, $level3Uids);

        $return = [
            'level1' => intval($level1Amount),
            'level2' => intval($level2Amount),
            'level3' => intval($level3Amount),
            'level1Uids' => $level1Uids,
            'level2Uids' => $level2Uids,
            'level3Uids' => $level3Uids
        ];
        $this->jsuccess($return);

    }

    public function userListByUids()
    {
        $uids = I("uids", '', 'op_t');
        $uids = json_decode($uids, true);
        $users = [];
        foreach ($uids as $uid) {
            $address = get_address($uid);
            $address = strMiddleReduceWordSensitive($address, 20);
            $users[] = [
                'address' => $address,
                'uid' => $uid,
            ];

        }
        $this->jsuccess($users);
    }

//    public function sharePostTest(){
//        $inviteCode = mt_rand(1000000, 9999999);
//        $url = getSharePageUrl() . '/App/Index/index?code=' . $inviteCode;
//        $postUrl = getApiUrl() . "/gen_qrcode/api.php";
//        $data['qrtext'] = urlencode($url);
//        $data['invite_code'] = $inviteCode;
//        $rs = curlPost($postUrl, $data);
//
//        $return = array(
//            'img_source' => getApiUrl() . $rs
//        );
//        $this->jsuccess($return);
//    }

    public function sharePost()
    {
        $uid = $this->getUid();
        $inviteCode = M("Invite")->where("uid={$uid} AND status=1")->getField("code");
        if (!$inviteCode) {//邀请码不存在，则重新生成
            $Invite = new InviteModel();
            $Invite->createCode($uid);
            $inviteCode = M("Invite")->where("uid={$uid} AND status=1")->getField("code");
        }

        $url = getSharePageUrl() . '/index.html?code=' . $inviteCode;
        $postUrl = getApiUrl() . "/gen_qrcode/api.php";
        $data['qrtext'] = urlencode($url);
        $data['invite_code'] = $inviteCode;
        $rs = curlPost($postUrl, $data);

        $return = array(
            'img_source' => getApiUrl() . $rs,
            'invite_code' => $inviteCode
        );
        $this->jsuccess($return);
    }


}